package com.ly.hotel.timetask;

import java.lang.ref.WeakReference;
import java.net.URLEncoder;
import java.util.TimerTask;

import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.google.gson.GsonBuilder;
import com.ly.hotel.common.MyApplication;
import com.ly.hotel.model.ModelBean;
import com.ly.hotel.service.SPABackService;
import com.ly.hotel.service.SPAService;

import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class CallStatusSyncTimerTask extends TimerTask{
	private ThisHandler handler = new ThisHandler(this);
	static MyApplication app = (MyApplication) MyApplication.getContext();
	@Override
	public void run() {
		// TODO Auto-generated method stub
		Message message = new Message();
        message.what=1;
        handler.sendMessage(message);
	}

	private static class ThisHandler extends Handler {
		private WeakReference<CallStatusSyncTimerTask> wrActivity;

		public ThisHandler(CallStatusSyncTimerTask activity) {
			wrActivity = new WeakReference<CallStatusSyncTimerTask>(activity);
		}

		@Override
		public void handleMessage(Message message) {
			super.handleMessage(message);
            if(message.what==1) {
            	try {
					//只有登陆了，获取到设备信息，才能正常请求呼叫状态 
					if(MyApplication.isLogined() && MyApplication.myLicenseGetValue("呼叫").equals(1)){
						String url = MyApplication.getHost() + SPAService.PROJECT + "/api/get-status.json?mac=" + MyApplication.getMAC() + "&content=";
						url += URLEncoder.encode("呼叫:"
								+ MyApplication.getDeviceInfo().getCategoryName() + "-" + MyApplication.getDeviceInfo().getName() + ","
								+ "呼叫");
//						HttpHelper.getStringAsync(getBaseContext(), url, null, handler);
						//Log.i("call", "请求呼叫状态:" + url); 
						StringRequest  stringRequest = new StringRequest(Request.Method.GET, url, 
				                 new Response.Listener<String>() {  
				                     @Override  
				                     public void onResponse(String response) {  
//				                         Log.d("call", response);  
				         				Log.v("call", "请求呼叫状态完成:" + response); 

				         				String rv = null;
				         				if(response != null){
				         					ModelBean d = new GsonBuilder().create().fromJson(response, ModelBean.class);
				         					if (d != null) {
				         						rv =  d.getData().toString();
//				         						Log.v("call", "请求呼叫状态完成:" + rv);
				         						if (rv != null) {
				         							if (rv.contains("处理完成") || rv.contains("取消请求") || rv.contains("处理超时")) {
//				         								Log.i("call", "服务器状态:处理完成或者取消请求");
				         								app.setLight(false);
				         							} else if (rv.contains("呼叫请求") || rv.contains("正在处理")) {
//				         								Log.i("call", "服务器状态:处理中");
				         								app.setLight(true);
				         							} else if (rv.equals("")){
				         								app.setLight(false);
				         								// app.setCallStatus(Constants.CALL_CANCLE);
				         							} 
				         						}else{
				         							Log.e("call", "呼叫状态:" + rv);
				         						}
				         					}
				         				}
				                     }  
				                 }, new Response.ErrorListener() {  
				                     @Override  
				                     public void onErrorResponse(VolleyError error) {  
				                    	 Log.e("call", "请求呼叫状态失败" + error.getMessage()); 
				                     }  
				                 }); 
						stringRequest.setTag("call");
						stringRequest.setShouldCache(false);
						SPABackService.mQueue.add(stringRequest); 
					}
//					SPAService.getCallStatus(getBaseContext(), MyApplication.getMAC(),handler);
					
				} catch (Exception e) {
					e.printStackTrace();
					Log.e("call", "获取呼叫状态失败:" + e.getMessage());
				}
            }
		}
	}
}
